package main

import (
	"fmt"
	"log"
	"regexp"
	"server/base"
	"strconv"
	"strings"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

type GameItem struct {
	No     int     `json:"no"`
	Name   string  `json:"name"`
	Size1  float64 `json:"size1"` // 安装前大小
	Size2  float64 `json:"size2"` // 安装后大小; 免安装的是安装后，安装版是安装前
	Genre  string  `json:"genre"`
	Letter string  `json:"letter"`
	Ver    float64 `json:"ver"` // 最小版本
	Added  bool    `json:"added"`
	Pinyin string  `json:"pinyin"`
}

type JsExporter struct {
}

// 字符串转化为float GB
func (self *JsExporter) fromSize2(s string) float64 {
	if s == "" {
		return 0
	}
	var sz, unit string
	for i, c := range s {
		if !(c >= '0' && c <= '9' || c == '.') {
			sz = s[:i]
			unit = strings.TrimSpace(s[i:])
			break
		}
	}
	f, err := strconv.ParseFloat(sz, 64)
	if err != nil {
		log.Println("warning fromSize2:", err)
	}
	switch unit {
	case "GB":
		return f
	case "MB":
		return f / 1024
	case "KB":
		return f / 1024 / 1024
	default:
		log.Println("warning fromSize2: unit不能识别", s)
	}
	return 0
}

// 降级版本
func (self *JsExporter) downVer(m string) float64 {
	if m == "" {
		return 0
	}
	l := strings.Split(m, "&")
	for _, s := range l {
		if strings.Contains(s, "[") {
			reg := regexp.MustCompile(`^(\d+)\[([\w.]+)\]$`)
			p := reg.FindStringSubmatch(s)
			if len(p) != 3 {
				log.Println("解析M失败", s)
				continue
			}
			t, err := strconv.Atoi(p[1])
			if err != nil {
				log.Println("解析M失败2", p[1], err)
				continue
			}
			v := p[2]
			f, _ := strconv.ParseFloat(v, 64)
			if t == 1 && f > 0 {
				return f
			}
		}
	}
	return 0
}

func (self *JsExporter) ReadExcel(filename string) []*GameItem {
	var out []*GameItem
	f, err := excelize.OpenFile(filename)
	if err != nil {
		log.Println("err:", err)
		return out
	}
	sheets := f.GetSheetList()
	no := 0
	last_genre := ""
	var last_ver float64
	for _, shname := range sheets {
		rows, err := f.GetRows(shname)
		base.Raise(err)
		for _, row := range rows[1:] {
			if len(row) <= 24 {
				fmt.Println("h1")
				continue
			}
			size1, _ := strconv.ParseFloat(row[10], 64)
			size2 := self.fromSize2(row[24])
			name := row[3]
			genre := row[7]
			if genre == "" {
				genre = last_genre
			} else {
				last_genre = genre
			}
			if size2 == 0 || name == "" {
				fmt.Println("h2", name, row[24])
				continue
			}
			py := base.PinyinString(name)
			letter := ""
			if len(py) > 0 {
				letter = strings.ToUpper(py[:1])
			}
			u := strings.TrimSpace(row[20]) // 最低版本 1.7,NA
			minver, _ := strconv.ParseFloat(u, 64)
			m := strings.TrimSpace(row[12]) // PATCH类型 1:降级 2:升级 3:金手指 4:中文 5:强制
			downver := self.downVer(m)
			if minver == 0 && downver != 0 {
				minver = downver
			}
			if minver == 0 {
				minver = last_ver
			} else {
				last_ver = minver
			}
			no += 1
			it := &GameItem{
				No:     no,
				Name:   name,
				Size1:  size1,
				Size2:  size2,
				Genre:  genre,
				Ver:    minver,
				Letter: letter,
				Pinyin: py,
			}
			log.Println(base.JsonString(it))
			out = append(out, it)
		}
	}
	return out
}
